﻿using AutoMapper;
using B.S.Backtrack.Domain.Entities;
using B.S.Backtrack.ErrorCode;
using B.S.Backtrack.Infrastructure.Interface;
using B.S.Backtrack.Write.API.Application.Command.Product;
using MediatR;

namespace B.S.Backtrack.Write.API.Application.CommandHandler.Product
{
    public class DelProductHandler : IRequestHandler<DelProductCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<Products> proRep;

        public DelProductHandler(IBaseRepository<Products> proRep)
        {
            this.proRep = proRep;
        }
        public Task<ApiResult<int>> Handle(DelProductCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            var pro = proRep.GetAll().Where(x=>x.ProductId==request.ProductId).FirstOrDefault();
            if (pro!=null)
            {
                pro.IsDel = true;
                proRep.Update(pro);
            }
           
            result.Code = ApiEnums.删除成功;
            result.Msg = ApiEnums.删除成功.ToString();
            result.Data = 1;
            return Task.FromResult(result);
        }
    }
}
